import { defineStore } from 'pinia';
import { signIn, getUserDetail } from 'src/api/user';
import { AxiosResponse } from 'axios';

interface userInfo  {
    username: string;
    is_admin?: boolean;
    [key: string]: any;
}

export const useUserStore = defineStore('user', {
    state: () => ({
        username: '',
        observers: <any>[]
    }),
    getters: {},
    actions: {
      addObserver(observer:NonNullable<unknown>) {
        this.observers.push(observer)
      },
      removeObserver:function (observer:any) {
        const index = this.observers.indexOf(observer);
        if(~index){
          this.observers.splice(index,1);
        }
      },
      notifyObservers:function (message:any) {
        for(let i = 0; i <= this.observers.length-1; i++){
          this.observers[i](message);
        }
      },
      persist: {
        enabled: true,
        strategies: [
          {
            storage: sessionStorage,
            paths: ['observers']
          }
        ]
      }
    },
});
